<template>
	<el-dialog title="新增" :visible.sync="dialogFormVisible" :before-close="handleDialogClose">
		<el-form :model="form" :rules="rules" ref="form">
			<el-form-item label="景点名称" :label-width="formLabelWidth" prop ="spotName">
				<el-input v-model="form.spotName" autocomplete="off" style="width: 50%;"></el-input>
			</el-form-item>
			<el-form-item label="景点图片" :label-width="formLabelWidth" prop="spotPicture">
				<el-upload class="orders-img_el_upload" :action="upurl" :limit="30" list-type="picture-card"
					:on-change="handleChange" :on-preview="handlePreview" :on-remove="handleRemove"
					:on-success="handleSuccess" :on-error="handleError" :file-list="fileList"
					:class="{ disUoloadSty: noneBtnImg }" multiple ref="upload">
					<span class="orders-img_el_upload_btn" @click.stop="submitUpload">添加图片</span>
				</el-upload>
				<el-dialog title="图片预览" :visible.sync="dialogVisible" :modal="false" top="10vh" class="previewImg">
					<img v-if="dialogImageUrl" width="100%" :src="dialogImageUrl" alt="预览图片" />
				</el-dialog>
			</el-form-item>
			<el-form-item label="联系电话" :label-width="formLabelWidth" prop="spotNumber">
				<el-input v-model="form.spotNumber" autocomplete="off" style="width: 20%;"></el-input>
			</el-form-item>
			<el-form-item label="所属地区" :label-width="formLabelWidth">
				<el-cascader 
				v-model="value"
				:props="{ value: 'regionName', label: 'regionName',checkStrictly: true}"
				:options="options" 
				collapse-tags
				clearable></el-cascader>
			</el-form-item>
			<el-form-item label="详细地址" :label-width="formLabelWidth" prop="spotLocation">
				<el-input v-model="form.spotLocation" autocomplete="off" style="width: 50%;"></el-input>
			</el-form-item>
			<el-form-item label="开放时间" :label-width="formLabelWidth" prop="openingHours">
				<el-input v-model="form.openingHours" autocomplete="off" style="width: 50%;"></el-input>
			</el-form-item>
			<el-form-item label="景点简介" :label-width="formLabelWidth" prop="spotBrief">
				<el-input v-model="form.spotBrief" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
			</el-form-item>
			<el-form-item label="公告信息" :label-width="formLabelWidth">
				<el-input v-model="form.announcement" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
			</el-form-item>
			<el-form-item label="景点介绍" :label-width="formLabelWidth">
				<div ref="editor"></div>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="cleardata">取 消</el-button>
			<el-button type="primary" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>

<script>
	import scenicSpotApi from "@/api/scenicSpot.js"
	import Editor from "wangeditor"
	export default {
		components: {

		},
		data() {
			return {
				editor: null,
				dialogImageUrl: "",
				dialogVisible: false,
				showBtnImg: true,
				noneBtnImg: false,
				limitCountImg: 30,
				upurl: "/api/file/upload/ScenicSpot",
				fileList: [],
				options:[],
				value:[],
				imgurl: "",
				dialogTableVisible: false,
				dialogFormVisible: false,
				form: {
					spotId: "",
					spotRegion:"",
					spotName:"",
					spotBrief:"",
					spotLocation:"",
					spotPicture:"",
					spotNumber:"",
					openingHours:"",
					announcement:"",
					spotIntroduce:"",
				},
				rules: {
					spotName: [{
						required: true,
						message: "请输入景点名",
						trigger: 'blur'
					}],
					spotBrief: [{
						required: true,
						message: "请输入简介",
						trigger: 'blur'
					}],
					spotLocation: [{
						required: true,
						message: "请输入详细地址",
						trigger: 'blur'
					}],
					spotPicture: [{
						required: true,
						message: "请上传景点图片",
						trigger: 'blur'
					}],
					spotNumber: [{
						required: true,
						message: "请输入官方电话",
						trigger: 'blur'
					}],
					openingHours: [{
						required: true,
						message: "请输入开放时间",
						trigger: 'blur'
					}],
				},
				formLabelWidth: '120px'
			};
		},
		methods: {
			createEditor() {
				this.$nextTick(() => {
					this.editor = new Editor(this.$refs.editor)
					this.editor.config.uploadImgServer = "api/file/editUpload/ScenicSpot"
					this.editor.config.uploadFileName = "file"
					this.editor.config.uploadVideoServer = 'api/file/editUpload/ScenicSpot'
					this.editor.config.uploadVideoName = "file"
					this.editor.create()
				})
			},
			handleError(err, file, fileList) {
				this.$message({
					message: "上传失败！",
					type: "success",
				});
				console.log(err);
			},
			handleSuccess(response, file, fileList) {
				if (response.flag === true && response.data) {
					const newImagePath = response.data;
					if (!this.form.spotPicture) {
						this.form.spotPicture = newImagePath;
					} else {
						// 如果路径还不在数组里，则添加
						if (!this.form.spotPicture.includes(newImagePath)) {
							this.form.spotPicture += ',' + newImagePath;
						}
					}
					// 或者如果要使用数组，可以这样：
					// this.form.spotPicture.push(newImagePath);
					this.fileList = fileList;
					if (fileList.length >= 30) {
						this.uploadDisabled = true;
					}
					this.$message({
						message: "上传成功",
						type: "success",
					});
				} else {
					this.$message({
						message: "上传失败",
						type: "error",
					});
				}
			},
			handleChange(file, fileList) {
				this.noneBtnImg = fileList.length >= this.limitCountImg;
			},
			handleRemove(file, fileList) {
				if (fileList.length <= 0) {
					this.form.spotPicture = "";
				}
			},
			handlePreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			submitUpload() {
				this.$refs.upload.submit();
			},
			cleardata() {
				this.dialogFormVisible = false;
				this.$refs.form.resetFields();
				this.value = [];
				this.fileList = [];
				this.editor.destroy();
				this.editor = null;
			},
			handleDialogClose() {
				this.dialogFormVisible = false;
				this.$refs.form.resetFields();
				this.value = [];
				this.fileList = [];
				this.editor.destroy();
				this.editor = null;
			},
			async AddScenic() {
				let content = this.editor.txt.html();
				const res = await scenicSpotApi.AddScenic("/znScenicspot/AddScenic", {
					spotRegion: this.value.join(','),
					spotName: this.form.spotName,
					spotBrief: this.form.spotBrief,
					spotLocation: this.form.spotLocation,
					spotPicture: this.form.spotPicture,
					spotNumber: this.form.spotNumber,
					openingHours: this.form.openingHours,
					announcement: this.form.announcement,
					spotIntroduce: content,
				});
				if (res.code == 200) {
					this.$message.success("添加成功");
					this.$parent.SearchScenicAll();
					this.dialogFormVisible = false;
				}
			},
			submitForm(form) {
				this.$refs[form].validate((valid) => {
					if (valid) {
						this.AddScenic();
						this.$refs.form.resetFields();
						this.value = [];
						this.fileList = [];
						this.editor.destroy();
						this.editor = null;
					} else {
						console.log("error submit!!");
						return false;
					}
				});
			},
		},
		created() {
			
		}
	};
</script>

<style lang="scss" scoped>
	.avatar {
		float: left;
		width: 148px;
		height: 148px;
		border-radius: 3%;
		margin-right: 10px;
	}
</style>